﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Xml;

namespace RESTful.DAO
{
    public class ChiTietDanhMuc_DAO : DataProvider
    {
        public ChiTietDanhMuc_DAO()
        {
            Connect();
        }

        public object ConvertDataTableToArray(DataTable dt, int i)
        {
            DTO.ChiTietDanhMuc_DTO data = new DTO.ChiTietDanhMuc_DTO();
            data.MaDanhMuc = Convert.ToInt32(dt.Rows[i]["MaDanhMuc"].ToString());
            data.Link = dt.Rows[i]["Link"].ToString();
            return (object)data;
        }

        public List<DTO.DataFeed_DTO> listFeed = new List<DTO.DataFeed_DTO>();

        public DTO.DataFeed_DTO[] GetFeed(String MaDanhMuc)
        {            
            try
            {                
                String sql = "Select * From ChiTietDanhMuc";
                sql += " Where TinhTrang = 1 and MaDanhMuc = " + MaDanhMuc;
                List<DTO.ChiTietDanhMuc_DTO> listDM = new List<DTO.ChiTietDanhMuc_DTO>();
                DataTable dt = ExecuteQuery(sql);                
                for (int i = 0; i < dt.Rows.Count; i++)
                    listDM.Add((DTO.ChiTietDanhMuc_DTO)ConvertDataTableToArray(dt, i));
                for (int i = 0; i < listDM.Count; i++)                
                    HandleFeed(listDM[i].Link.ToString());

                DTO.DataFeed_DTO[] data = new DTO.DataFeed_DTO[listFeed.Count];
                for (int i = 0; i < listFeed.Count; i++)
                    data[i] = (DTO.DataFeed_DTO)ConvertListToArray(i);
                return data;               
            }
            catch
            {
                DisConnect();
                return null;
            }            
        }

        public object ConvertListToArray(int i)
        {
            DTO.DataFeed_DTO data = new DTO.DataFeed_DTO();
            data.Link = listFeed[i].Link.ToString();
            data.PubDate = listFeed[i].PubDate.ToString();
            data.Title = listFeed[i].Title.ToString();
            data.Description = listFeed[i].Description.ToString();
            return (object)data;
        }

        public object ConvertFeed(DataTable dt, int i)
        {
            DTO.DataFeed_DTO data = new DTO.DataFeed_DTO();
            data.Title = dt.Rows[i]["Title"].ToString();
            data.Link = dt.Rows[i]["Link"].ToString();
            data.Description = dt.Rows[i]["Description"].ToString();
            data.PubDate = dt.Rows[i]["PubDate"].ToString();
            return (object)data;
        }

        public void HandleFeed(String url)
        {
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(url);
                DataSet ds = new DataSet();
                ds.ReadXml(reader);
                DataTable dt = ds.Tables["item"];
                DTO.DataFeed_DTO[] data = new DTO.DataFeed_DTO[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    data[i] = (DTO.DataFeed_DTO)ConvertFeed(dt, i);
                    listFeed.Add(data[i]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }
}